package sample.servlet;

import fri.businessLayer.UserBeanRemote;
import fri.entiry.FrUser;
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class RegisterController extends HttpServlet {

    @EJB
    private UserBeanRemote userBeanRemote;
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String action = request.getParameter("action");
            if (action != null) {
                if (action.equals("Register")) {
                    String txtUserId        = request.getParameter("txtUserId");
                    String txtEmail         = request.getParameter("txtEmail");
                    String txtFullName      = request.getParameter("txtFullName");
                    String txtUserCode      = request.getParameter("txtUserCode");
                    String txtAvatar        = request.getParameter("txtAvatar");
                    String txtPassword      = request.getParameter("txtPassword");
                    String txtDateCreated   = request.getParameter("txtDateCreated");
                    String txtLastEffect    = request.getParameter("txtLastEffect");
                    
                    FrUser frUser = new FrUser();
                    frUser.setUserId(txtUserId);
                    frUser.setEmail(txtEmail);
                    frUser.setFullName(txtFullName);
                    frUser.setUserCode(txtUserCode);
                    frUser.setAvatar(txtAvatar);
                    frUser.setPassword(txtPassword);
                    frUser.setDateCreated(null);
                    frUser.setLastEffect(null);
                    
                    if (userBeanRemote.addUser(frUser) != 0) {
                        HttpSession session = request.getSession(true);
                        session.setAttribute("STATUS_LOGIN", 1);
                        session.setAttribute("USER_LOGIN", frUser);
                        RequestDispatcher rd = request.getRequestDispatcher("reqHome.jsp");
                        rd.forward(request, response);
                    }
                }
            }
            RequestDispatcher rd = request.getRequestDispatcher("reqRegister.jsp");
            rd.forward(request, response);
        } finally {
        }
    }
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
